package com.boilerCloud.controller.sys;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.boilerCloud.dataSource.LogAnnotation;
import com.boilerCloud.dto.HzlqswPage;
import com.boilerCloud.dto.sys.Dictionary;
import com.boilerCloud.dto.sys.ProtocolInfo;
import com.boilerCloud.dto.sys.ReqResult;
import com.boilerCloud.service.sys.IProtocolService;


@RestController
@RequestMapping("/protocol/")
public class ProtocolControler {
	private static final Logger log = LoggerFactory.getLogger(ProtocolControler.class);
	@Resource
	private IProtocolService service;


	@RequestMapping(value = "protocolList")
	public ModelAndView dictionaryList() {
		return new ModelAndView("/views/sys/protocolList.html");
	}
	
	

	/**
	 * 查询协议列表
	 * @param grade
	 * @param yema
	 * @param length
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "qryProtocolListList", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult qryProtocolListList(@ModelAttribute ProtocolInfo model ,
			@RequestParam(required = true) int yema,
			@RequestParam(required = true) int length,HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			
			HzlqswPage<ProtocolInfo> page = new HzlqswPage<ProtocolInfo>();
			page.setCurPage(yema);
			page.setPageSize(length);
			service.qryList(model,page);
			rst.setReturnObject(page);
			rst.setResultCode(ReqResult.resultCode_Success);
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("查询协议列表失败");
			log.error("查询协议列表异常",e);
			return rst;
		}
	}
	
	/**
	 * 修改协议信息
	 * @param device
	 * @param request
	 * @return
	 */
	@LogAnnotation("修改协议")
	@RequestMapping(value = "editProtocolInfo", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult editDictionary(
			@ModelAttribute ProtocolInfo model ,
			HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			service.edit(model);
			rst.setResultCode(ReqResult.resultCode_Success);
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("修改协议信息失败");
			log.error("修改协议信息异常",e);
			return rst;
		}
	}
	/**
	 * 新增协议
	 * @param device
	 * @param request
	 * @return
	 */
	@LogAnnotation("新增协议")
	@RequestMapping(value = "addProtocolInfo", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult addDictionary(
			@ModelAttribute ProtocolInfo model ,
			HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			service.save(model);
			rst.setResultCode(ReqResult.resultCode_Success);
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("新增协议信息失败");
			log.error("新增协议信息异常",e);
			return rst;
		}
	}
	
	/**
	 * 删除协议
	 * @param businessNo
	 * @param request
	 * @return
	 */
	@LogAnnotation("删除协议")
	@RequestMapping(value = "delProtocolInfo", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult delModel(
			@RequestParam(required = true) int id,
			HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			service.del(id);
			rst.setResultCode(ReqResult.resultCode_Success);
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("删除协议失败");
			log.error("删除协议异常",e);
			return rst;
		}
	}
	
	@RequestMapping(value = "getProtocolInfo", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult getProtocolInfo(
			@RequestParam(required = true) int id,
			HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			ProtocolInfo model = service.getProtocolInfo(id);
			rst.setResultCode(ReqResult.resultCode_Success);
			rst.setReturnObject(model);
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("删除协议失败");
			log.error("删除协议异常",e);
			return rst;
		}
	}
	
	
	

}
